﻿using System.Xml;

namespace Oblivion.Substructure.InformationManagement
{
    /// <summary> This class represents an XML Document.  </summary>
    public sealed class XmlFile : XmlDocument
    {
        // Local-Scope Variable Declarations:
        private readonly string XmlNamespace;

        /// <summary> This class represents an XML Document.  </summary>
        /// <param name="path">The path of the document.</param>
        public XmlFile(string path, string xmlNamespace)
        {
            Load(path);
            XmlNamespace = xmlNamespace;
        }

        /// <summary> This method allows the programmer to read a selection from an XML node and return a string. </summary>
        /// <param name="section">The node (ex: [MySql][/MySql]) of the XmlNamespace (most likely "Configuration").</param>
        /// <param name="field">The field of the node (ex: [Host][/Host]).</param>
        /// <returns></returns>
        public string ReadString(string section, string field)
        {
            return SelectSingleNode(XmlNamespace + "/" + section + "/" + field).InnerText;
        }

        /// <summary> This method allows the programmer to read a selection from an XML node and return a /signed/ integer. </summary>
        /// <param name="section">The node (ex: [MySql][/MySql]) of the XmlNamespace (most likely "Configuration").</param>
        /// <param name="field">The field of the node (ex: [Host][/Host]).</param>
        /// <returns></returns>
        public int ReadInt32(string section, string field)
        {
            int result = 0;
            int.TryParse(ReadString(section, field), out result);
            return result;
        }

        /// <summary>
        /// This method alls the programmer to read a selection from an XML node and return a boolean
        /// </summary>
        /// <param name="section">The node (ex: [MySql][/MySql]) of the XmlNamespace (most likely "Configuration").</param>
        /// <param name="field">The field of the node (ex: [Host][/Host]).</param>
        /// <returns></returns>
        public bool ReadBool(string section, string field)
        {
            bool result;
            bool.TryParse(ReadString(section, field), out result);
            return result;
        }
    }
}